import App from './App'
import messages from './locale/index'
let i18nConfig = {
  locale: uni.getLocale(),
  messages
}
// 引入封装后的axios

Vue.prototype.Httpus = 'https://app3.wx-api.online/'
Vue.prototype.app_ad_ys_cp='adunit-f9acaa79884b0916' 


// main.js

  
// #ifndef VUE3
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n(i18nConfig)
Vue.config.productionTip = false
App.mpType = 'app'
console.log(uni.getLocale())
const app = new Vue({
  i18n,
  ...App
})

// #endif
Vue.prototype.hesplic = function(da) {
  let arr = da.split('&')

  var obj = {}

  for (var i = 0; i < arr.length; i++) {
    // 把每一项再按照=拆分 得到左边的当成对象key 右边当成对象value
    var sonArr = arr[i].split(':')
    obj[sonArr[0].replace(/[\'\"\\\/\b\f\n\r\t]/g,'')] = sonArr[1].replace(/[\'\"]/g,"").trim();
  }
  return obj
}
Vue.prototype.postsplic = function(da) {
  let arr = da.split('&')

  var obj = {}
  for (var i = 0; i < arr.length; i++) {
    // 把每一项再按照=拆分 得到左边的当成对象key 右边当成对象value
    var sonArr = arr[i].split('=')
    obj[sonArr[0]] = sonArr[1]
  }
  return obj
}
Vue.prototype.iddecs = function(arr) {
  if (arr) {
    let result = [],
      arr1 = [];

    result = arr.filter(function(element, index, self) {
      return self.findIndex((el) => el.sortid == element.sortid) === index;
    });
    for (let i = 0; i < result.length; i++) {
      arr1.push(result[i].sortid);
    }
    // 排序 b-a 由大到小; a-b 由小到大
    var cur = arr1.sort((a, b) => {
      return b - a;
    });

    var push = [];
    for (let item in result) {
      if (in_array(result[item].sortid, cur)) {
        result[item]["sort"] = cur.indexOf(result[item].sortid);
        push.push(result[item]);
      }
    }
    push = push.sort(function(a, b) {
      if (a.sort < b.sort) {
        return -1;
      } else if (a.sort > b.sort) {
        return 1;
      }
      return 0;
    });

    function in_array(key, array) {
      for (var i in array) {
        if (array[i] == key) {
          return true;
        }
      }
      return false;
    }
    return push.reverse();
  }
}
Vue.prototype.textid = function(arr) {
  if (arr) {
    let result = [],
      arr1 = [];

    result = arr.filter(function(element, index, self) {
      return self.findIndex((el) => el.id == element.id) === index;
    });
    for (let i = 0; i < result.length; i++) {
      arr1.push(result[i].id);
    }
    // 排序 b-a 由大到小; a-b 由小到大
    var cur = arr1.sort((a, b) => {
      return b - a;
    });

    var push = [];
    for (let item in result) {
      if (in_array(result[item].id, cur)) {
        result[item]["sort"] = cur.indexOf(result[item].id);
        push.push(result[item]);
      }
    }
    push = push.sort(function(a, b) {
      if (a.sort < b.sort) {
        return -1;
      } else if (a.sort > b.sort) {
        return 1;
      }
      return 0;
    });

    function in_array(key, array) {
      for (var i in array) {
        if (array[i] == key) {
          return true;
        }
      }
      return false;
    }
    return push.reverse();
  }
}
Vue.prototype.parseParam = function(params) {
  return Object.keys(params).map(key => key + '=' + params[key]).join('&');;
}
Vue.prototype.dbs = 'gr_book'

//封装的异步请求（H5不可直接请求，会报跨域错误，需要浏览器进行设置，或者API允许跨域。APP直接使用即可）
Vue.prototype.zaiApi = function(promise, Loading = true) {
 
  let header = {}
  if (promise.header) {
    header = promise.header
  }
  let data = {}
  if (promise.data) {
    data = promise.data
  }
 // let pm = promise.url+(JSON.stringify(data) !== "{}" ? '?' + Vue.prototype.parseParam(data) : '')
 let pm = promise.url
 
 
  // #ifdef  APP-PLUS

 
  return new Promise(function(resolve, reject) {
    uni.request({
      url: pm, 
      data: data,
      timeout: 15000,
      method: promise.method ? promise.method : 'GET',
      header: header,
      sslVerify: false,
      success: (res) => {
        resolve(res.data);
      },
      fail(e) {
        reject(e)
      },
      complete(e) {
       //console.log('complete', e)
      }
    });
  });
   // #endif
   
   // #ifdef MP || H5
   let config= uni.getStorageSync('config')
   let das={}
   let http=pm?(pm.indexOf('Home-Ad')>=0 ?0:1):0;
  console.log('-config',http,pm)
   if(config && config.wx_http && http){
	
	   das.url=pm
	   das.data=data
	   das.header=header
	   das.method=promise.method ? promise.method : 'GET'
	   let method= 'POST'
	   if(pm.includes(".json")){
	   		  method='GET' 
	   }
		config.wx_http_url=pm.includes(".json")?pm:config.wx_http_url
			// console.log('pm',config.wx_http_url,pm.includes(".json"))
	   return new Promise(function(resolve, reject) {
	   Vue.prototype.zaistop=uni.request({
	       url: config.wx_http_url, 
	       data: das,
	       timeout: 15000,
	       method:method,
	       header: {
					'content-type': 'application/json' //自定义请求头信息
				},
	       sslVerify: false,
	       success: (res) => {
	         resolve(res.data);
	       },
	       fail(e) {
	         reject(e)
	       },
	       complete(e) {
	        //console.log('complete', e)
	       }
	     });
	   });
   }else{
  console.log(data)
   return new Promise(function(resolve, reject) {
     Vue.prototype.zaistop=uni.request({
       url: pm, 
       data: data,
       timeout: 15000,
       method: promise.method ? promise.method : 'GET',
       header: header,
       sslVerify: false,
       success: (res) => {
		  
         resolve(res.data);
       },
       fail(e) {
		   
         reject(e)
       }
     });
   });
   }
    // #endif
};





// #ifdef VUE3
import {
  createSSRApp
} from 'vue'
import {
  createI18n
} from 'vue-i18n'
const i18n = createI18n(i18nConfig)
export function createApp() {
  const app = createSSRApp(App)
  app.use(i18n)
  return {
    app
  }
}
// #endif



app.$mount()
